import * as $task from '@/api/task'
import {UI_SET_IS_SHOW_MODEL, UPDATE_IS_LOADING, UI_SET_PAGE_INFO} from './ui'

export const TASK_UPDATE_TASKS = 'TASK_UPDATE_TASKS'
export const TASK_UNSHIFT_TASKS = 'TASK_UNSHIFT_TASKS'
export const TASK_SET_TASKS = 'TASK_SET_TASKS'
export const TASK_SET_TASK = 'TASK_SET_TASK'
export const TASK_RESET_TASK_UNREAD_COUNT = 'TASK_RESET_TASK_UNREAD_COUNT'
export const TASK_RESET_TASKS_UNREAD_COUNT = 'TASK_RESET_TASKS_UNREAD_COUNT'
export const TASK_RESET_TASK_PROGRESSES_UNREAD_COUNT = 'TASK_RESET_TASK_PROGRESSES_UNREAD_COUNT'
export const TASK_UPDATE_TASK_PROGRESSES_LIKED = 'TASK_UPDATE_TASK_PROGRESSES_LIKED'
export const TASK_SET_URGE_RS = 'TASK_SET_URGE_RS'
export const TASK_UPDATE_TASKS_DOCUMENT = 'TASK_UPDATE_TASKS_DOCUMENT'
export const TASK_SET_UNREADCOUNTS = 'TASK_SET_UNREADCOUNTS'

const state = {
  tasks: [],
  task: {
    group: {
      openGroupId: ''
    },
    user: {
      id: 0
    },
    isExpired: true,
    expireAt: null,
    document: {
      data: {
        blocks: []
      }
    },
    progresses: [],
  },
  urgeRs: [],
  unreadCounts: []
}

const getters = {
  count: state => state.count,
  tasks: state => state.tasks,
  task: state => state.task,
  urgeRs: state => state.urgeRs,
  unreadCounts: state => state.unreadCounts,
}

const actions = {
  async fetchAllByDid ({ commit, state }, documentId) {
    let tasks = await $task.getAllByDid(documentId)
    commit(TASK_UPDATE_TASKS, tasks)
  },
  async fetchById ({ commit, state }, taskId) {
    let task = await $task.getById(taskId)
    commit(TASK_SET_TASK, task)
    return task
  },
  async getAllByIds ({ commit, state }, taskIds) {
    let tasks = await $task.getAllByIds(taskIds)
    commit(TASK_SET_TASKS, tasks)
    return tasks
  },
  async urge ({ commit, state }, taskId) {
    commit(UPDATE_IS_LOADING, true, {root: true})
    let rs = await $task.urge(taskId)
    commit(UPDATE_IS_LOADING, false, {root: true})
    commit(TASK_SET_URGE_RS, rs)
    commit(UI_SET_IS_SHOW_MODEL, true, {root: true})
  },
  async getMyCreatesByPage ({ commit, state }, {type = 'published', currentPage = 1}) {
    let rs = await $task.getByPage({type, currentPage})
    if (currentPage === 1) {
      commit('TASK_SET_TASKS', rs.rows)
    } else {
      commit('TASK_UPDATE_TASKS', rs.rows)
    }
    commit('UI_SET_PAGE_INFO', rs.page, {root: true})
    return rs
  },
}

const mutations = {
  TASK_UPDATE_TASKS (state, tasks) {
    state.tasks = [...state.tasks, ...tasks]
  },
  TASK_UNSHIFT_TASKS (state, tasks) {
    state.tasks = [...tasks, ...state.tasks]
  },
  TASK_SET_TASKS (state, tasks) {
    state.tasks = tasks
  },
  TASK_SET_TASK (state, task) {
    state.task = task
  },
  TASK_SET_UNREADCOUNTS (state, {id, unreadCount}) {
    state.unreadCounts.push({id, unreadCount})
  },
  TASK_RESET_TASK_UNREAD_COUNT (state) {
    state.task.unreadCount = 0
  },
  TASK_RESET_TASKS_UNREAD_COUNT (state, {taskId, unreadCount}) {
    if (!state.tasks) return
    if (state.tasks.length === 0) return
    let idx = -1
    for (var i = 0; i < state.tasks.length; i++) {
      let t = state.tasks[i]
      if (t.id === taskId) {
        idx = i
        break
      }
    }
    if (idx === -1) return
    state.tasks[idx].unreadCount = unreadCount
  },
  TASK_UPDATE_TASKS_DOCUMENT (state, {id, document}) {
    let idx = -1
    for (var i = 0; i < state.tasks.length; i++) {
      let t = state.tasks[i]
      if (t.id === id) {
        idx = i
        break
      }
    }
    if (idx === -1) return
    state.tasks[idx].document = document
  },
  TASK_RESET_TASK_PROGRESSES_UNREAD_COUNT (state, progressId) {
    let t = []
    state.task.progresses.forEach(n => {
      if (n.id === progressId) {
        n.unreadCount = 0
      }
      t.push(n)
    })
    state.task.progresse = t
  },
  TASK_UPDATE_TASK_PROGRESSES_LIKED (state, {progressId, flag}) {
    let t = []
    state.task.progresses.forEach(n => {
      if (n.id === progressId) {
        n.liked = flag
      }
      t.push(n)
    })
    state.task.progresse = t
  },
  TASK_SET_URGE_RS (state, rs) {
    state.urgeRs = rs
  }
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
